import axios from 'axios'
import { Alert, Space ,message} from 'antd';
import {useNavigate} from "react-router-dom";

// 创建
const request = axios.create({
    baseURL:"http://localhost:5173/api",
    timeout:3000
})
// 请求拦截
request.interceptors.request.use(function(config){
    config.headers.Authorization = "Bearer "+localStorage.getItem("token");
    return config;
})
// 响应兰拦截
request.interceptors.response.use(function(res){
    if(res.data.code==0){
        // <Alert message="Warning Text" type="warning" />
        message.error(res.data.msg)
    }
    return res
},function(err){
    if(err.response.status===422||err.response.status===401){
        message.warning("登录超时")
        window.location.href = "/login"
        //跳转登录页
        //const navigate = useNavigate()
        // //reactRouter默认状态下并不支持组件之外完成路由跳转
        // navigate('/login')
    }
    return Promise.reject(err)
})
// 导出
export default request;
